﻿/*
 * Copyright (c) 2006-2013, JGraph Ltd
 *
 * Defines the startup sequence of the application.
 */
{

	/**
	 * Constructs a new application (returns an mxEditor instance)
	 */

    function createEditor(config) {

        var editor = null;

        var hideSplash = function () {
            // Fades-out the splash screen
            var splash = document.getElementById('splash');

            if (splash != null) {
                try {
                    mxEvent.release(splash);
                    mxEffects.fadeOut(splash, 100, true);
                }
                catch (e) {
                    splash.parentNode.removeChild(splash);
                }
            }
        };

        try {
            if (!mxClient.isBrowserSupported()) {
                mxUtils.error('Browser is not supported!', 200, false);
            }
            else {
                mxObjectCodec.allowEval = true;
                var node = mxUtils.load(config).getDocumentElement();
                editor = new mxEditor(node);

                mxObjectCodec.allowEval = false;

                // Adds active border for panning inside the container
                editor.graph.createPanningManager = function () {
                    var pm = new mxPanningManager(this);
                    pm.border = 30;

                    return pm;
                };

                editor.graph.allowAutoPanning = true;
                editor.graph.timerAutoScroll = true;
                //加载数据
                if (mxGraph.WorkFlowId > 0 && $("#DesignXml").val().length > 0) {

                    var doc = mxUtils.parseXml(unescape($("#DesignXml").val()));
                 
                    editor.readGraphModel(doc.documentElement);
                }


                // Updates the window title after opening new files
                var title = document.title;
                var funct = function (sender) {
                    document.title = title + ' - ' + sender.getTitle();
                };

                editor.addListener(mxEvent.OPEN, funct);

                // Prints the current root in the window title if the
                // current root of the graph changes (drilling).
                editor.addListener(mxEvent.ROOT, funct);
                funct(editor);

                editor.addListener(mxEvent.SAVE, function () {
                    var enc1 = new mxCodec(mxUtils.createXmlDocument());
                    var node1 = enc1.encode(editor.graph.getModel());
                    var xml1 = mxUtils.getXml(node1);
                    alert(xml1);
                    $.post("/Platform/Workflow/SaveDesignXML",
                        { "xml": escape(xml1), id: mxGraph.WorkFlowId },
                        function (res) {
                            if (res.error.length === 0) {
                                alert("保存成功。");
                            } else {
                                alert(res.error);
                            }
                        });
                });

                editor.createProperties = function (cell) {

                    var model = this.graph.getModel();
                    var snode = model.getValue(cell);
                    var form = new mxForm('properties');
                    if (mxUtils.isNode(snode)) {
                        // Adds a readonly field for the cell id
                        var id = form.addText('ID', cell.getId());
                        id.setAttribute('readonly', 'true');
                        form.addText("name", cell.value);


                        showActivityPropertyDialog(form, snode);
                    }

                    //if (mxUtils.isNode(snode)) {
                    //    kmain.mxSelectedDomElement.Cell = cell;

                    //    if (model.isVertex(cell)) {
                    //        if (snode.nodeName === "Activity") {
                    //            kmain.mxSelectedDomElement.ElementType = 'Activity';
                    //            var activity = kmain.mxSelectedDomElement.Element = convert2ActivityObject(cell);
                    //            showActivityPropertyDialog(activity);
                    //        } else if (snode.nodeName === "Swimlane") {
                    //            ;
                    //        }
                    //    } else if (model.isEdge(cell)) {
                    //        if (snode.nodeName === "Transition") {
                    //            //transition page
                    //            kmain.mxSelectedDomElement.ElementType = 'Transition';
                    //            var transition = kmain.mxSelectedDomElement.Element = convert2TransitionObject(cell);
                    //            BootstrapDialog.show({
                    //                title: '转移属性',
                    //                message: $('<div></div>').load('/platform/transition/edit'),
                    //                data: { "node": transition },
                    //                draggable: true
                    //            });
                    //        }
                    //    }
                    //}

                    // Defines the function to be executed when the
                    // OK button is pressed in the dialog
                    var okFunction = mxUtils.bind(this, function () {
                        // Hides the dialog
                        this.hideProperties();

                        // Supports undo for the changes on the underlying
                        // XML structure / XML node attribute changes.
                        model.beginUpdate();
                        try {
                            if (geo != null) {
                                geo = geo.clone();

                                geo.x = parseFloat(xField.value);
                                geo.y = parseFloat(yField.value);
                                geo.width = parseFloat(widthField.value);
                                geo.height = parseFloat(heightField.value);

                                model.setGeometry(cell, geo);
                            }

                            // Applies the style
                            if (style.value.length > 0) {
                                model.setStyle(cell, style.value);
                            }
                            else {
                                model.setStyle(cell, null);
                            }

                            // Creates an undoable change for each
                            // attribute and executes it using the
                            // model, which will also make the change
                            // part of the current transaction
                            for (var i = 0; i < attrs.length; i++) {
                                var edit = new mxCellAttributeChange(
                                    cell, attrs[i].nodeName,
                                    texts[i].value);
                                model.execute(edit);
                            }

                            // Checks if the graph wants cells to 
                            // be automatically sized and updates
                            // the size as an undoable step if
                            // the feature is enabled
                            if (this.graph.isAutoSizeCell(cell)) {
                                this.graph.updateCellSize(cell);
                            }
                        }
                        finally {
                            model.endUpdate();
                        }
                    });

                    // Defines the function to be executed when the
                    // Cancel button is pressed in the dialog
                    var cancelFunction = mxUtils.bind(this, function () {
                        // Hides the dialog
                        this.hideProperties();
                    });

                    form.addButtons(okFunction, cancelFunction);

                    return form.table;
                }

                // Displays version in statusbar
                //editor.setStatus('mxGraph ' + mxClient.VERSION);

                // Shows the application
                hideSplash();
            }
        }
        catch (e) {
            hideSplash();

            // Shows an error message if the editor cannot start
            mxUtils.alert('Cannot start application: ' + e.message);
            throw e; // for debugging
        }

        return editor;
    }

    function showActivityPropertyDialog(form, snode) {
        if (snode.tagName === "Task"
            || snode.tagName === "MULTIPLEINSTANCE") {
            //活动属性
        } else if (snode.tagName === "GATEWAY") {
            //网关决策属性
        } else if (snode.tagName === "Subprocess") {
            //子流程属性
        } else if (snode.tagName === "START"
            || snode.tagName === "END") {
            ;
        }
        else {

            alert("未知节点类型" + snode.tagName);
            return false;
        }
    }

}
